package nl.ViewerServer.ServerManager.Paraview;

import java.net.MalformedURLException;
import java.rmi.RemoteException;

import javax.xml.namespace.QName;

import nl.ViewerServer.Exceptions.ParaviewConnectionException;

import org.apache.axis.AxisFault;
import org.apache.axis.client.Call;

public class SoapCalls {

   public static Object callMethod( Call c, String endpoint, String method, QName returnType, String[] paramNames,
         Object[] params ) throws ParaviewConnectionException {
      try {
         c.clearHeaders();
         c.clearOperation();

         c.setTargetEndpointAddress( new java.net.URL( endpoint ) );
         c.setOperationName( new QName( "", method ) );

         for( int i = 0; paramNames != null && i < paramNames.length; i++ ) {
            c.addParameter( paramNames[i], org.apache.axis.Constants.XSD_ANY, javax.xml.rpc.ParameterMode.IN );
         }

         c.setReturnType( returnType );

         if( params == null ) {
            params = new Object[] {};
         }
         Object ret = c.invoke( params );

         return ret;
      }
      catch( AxisFault a ) {
         throw new ParaviewConnectionException( a );
      }
      catch( MalformedURLException m ) {
         throw new ParaviewConnectionException( m );
      }
      catch( RemoteException re ) {
         throw new ParaviewConnectionException( re );
      }
   }
}
